/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package models;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import tableModels.TableCircuitModel;
import utils.Connexion;
import utils.TableModel;

/**
 *
 * @author juju
 */
public class CircuitMapper {
    private Connexion _connexion;
    
    public CircuitMapper() {
        try {
            this._connexion = new Connexion();
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public TableCircuitModel fetchAll() {
        String requete = "SELECT IDENTIFIANT, CONCAT(PAYSDEPART,CONCAT(' / ' ,VILLEDEPART)) as DEPART,CONCAT(PAYSARRIVEE, CONCAT(' / ',VILLEARRIVEE)) as ARRIVEE, DATEDEPART, NBRPLACEDISPONIBLE, PRIXINSCRIPTION FROM PROJET.CIRCUIT";
        String[] titre = {"ID","Départ","Arrivée","Date de départ", "Place disponibles", "Tarif"};
        TableCircuitModel model = null;
        try {
            model = new TableCircuitModel(this._connexion,requete,titre);
            model.getRowCount();
        } catch (SQLException ex) {
            Logger.getLogger(ClientMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
            return model;
    }
    
    private void update(Circuit circuit) {
        try {
            String requete = "UPDATE PROJET.CIRCUIT SET DESCRIPTIF='" + circuit.getDescriptif() + "',"
                    + "VILLEDEPART='" + circuit.getVilleDepart() +"', PAYSDEPART='" + circuit.getPaysDepart() +"',"
                    + "VILLEARRIVEE='" + circuit.getVilleArrivee() + "',PAYSARRIVEE='" + circuit.getPaysArrivee() + "',"
                    + "DATEDEPART='" + circuit.getDateDepart() + "',NBRPLACEDISPONIBLE='" + circuit.getNbrPlaceDisponible() + "',"
                    + "DUREE='" + circuit.getDuree() + "',PRIXINSCRIPTION='" + circuit.getPrixInscription() + "'"
                    + " WHERE IDENTIFIANT=" + circuit.getIdentifiant();
            this._connexion.setUpdate(requete);
        } catch (SQLException ex) {
            Logger.getLogger(CircuitMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public Circuit find(int id, Circuit circuit) throws SQLException  {
        String requete = "SELECT IDENTIFIANT,DESCRIPTIF, VILLEDEPART,PAYSDEPART,VILLEARRIVEE,PAYSARRIVEE, DATEDEPART,"
                + "NBRPLACEDISPONIBLE, DUREE, PRIXINSCRIPTION FROM PROJET.CIRCUIT WHERE IDENTIFIANT=" + id;
        this._connexion.setQuery(requete);
        ResultSet resultat = this._connexion.getResultSet();
        while(resultat.next()){
            circuit.setIdentifiant(Integer.parseInt(resultat.getString("IDENTIFIANT")));
            circuit.setDateDepart(resultat.getString("DATEDEPART"));
            circuit.setDescriptif(resultat.getString("DESCRIPTIF"));
            circuit.setDuree(Integer.parseInt(resultat.getString("DUREE")));
            circuit.setNbrPlaceDisponible(Integer.parseInt(resultat.getString("NBRPLACEDISPONIBLE")));
            circuit.setPaysArrivee(resultat.getString("PAYSARRIVEE"));
            circuit.setPaysDepart(resultat.getString("PAYSDEPART"));
            circuit.setPrixInscription(Integer.parseInt(resultat.getString("PRIXINSCRIPTION")));
            circuit.setVilleArrivee(resultat.getString("VILLEARRIVEE"));
            circuit.setVilleDepart(resultat.getString("VILLEDEPART"));
        }
        return circuit;
    }
    
    public void save(Circuit circuit) {
        if(circuit.getIdentifiant()== 0){
            this.add(circuit);
        } else {
           this.update(circuit);
        }
    }
    
    private void add(Circuit circuit) {
        try {
            String requete = "INSERT INTO PROJET.CIRCUIT VALUES(0,'" + circuit.getDescriptif() + "',"
                    + "'" + circuit.getVilleDepart() + "','" + circuit.getPaysDepart() +"',"
                    + "'" + circuit.getVilleArrivee() + "','" + circuit.getPaysArrivee() + "',"
                    + "'" + circuit.getDateDepart() + "','" + circuit.getNbrPlaceDisponible() + "',"
                    + "'" + circuit.getDuree() + "','" + circuit.getPrixInscription() + "')";
            this._connexion.setUpdate(requete);
        } catch (SQLException ex) {
            Logger.getLogger(CircuitMapper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}